package com.mdlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdlaser.entity.WishLanguage;
import com.mdlaser.mapper.WishLanguageMapper;
import com.mdlaser.service.IWishLanguageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mdlaser
 * @since 2024-05-16
 */
@Service
public class WishLanguageServiceImpl extends ServiceImpl<WishLanguageMapper, WishLanguage> implements IWishLanguageService {

    @Override
    public List<WishLanguage> selectByWishId(Integer wishId) {
        LambdaQueryWrapper<WishLanguage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WishLanguage::getWishId, wishId);
        return this.getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public String getLanguageDetailById(String languageId) {
        WishLanguage wishLanguage = this.getBaseMapper().selectById(languageId);
        if (wishLanguage != null) {
            return wishLanguage.getLanguage();
        }
        return null;
    }

    @Override
    @Cacheable("getMaxDurationLanguage")
    public WishLanguage getMaxDurationLanguage() {
        LambdaQueryWrapper<WishLanguage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WishLanguage::getDuration);
        List<WishLanguage> wishLanguages = this.getBaseMapper().selectList(queryWrapper);
        return CollectionUtils.isEmpty(wishLanguages) ? null : wishLanguages.get(0);
    }
}
